package com.wyzz.hr.report.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletRequest;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Empsalarydatadef;
import com.wyzz.hr.pojo.Jobgrade;
import com.wyzz.hr.report.action.support.SalaryReportParamBean;
import com.wyzz.hr.report.bo.SalaryReportBo;
import com.wyzz.hr.spring.SpringBeanFactory;

public class SalaryHistoryReport extends BaseAction {

	public SalaryHistoryReport() {
		reportFormat = "HTML";
	}

	public String execute() throws Exception {
		ServletRequest request = getRequest();
		if ("PDF".equalsIgnoreCase(reportFormat)) {
			request.setAttribute("_format", "PDF");
			request.setAttribute("_contentType", "attachment");
			request.setAttribute("_fileName", "empReport");
		} else {
			request.setAttribute("_format", "HTML");
		}
		SalaryReportBo salaryReportBo = (SalaryReportBo) SpringBeanFactory
				.getBean("salaryReportBo");
		Empsalarydatadef dataDef = (Empsalarydatadef) salaryReportBo
				.loadObject(Empsalarydatadef.class, bean.getItemId(), null);
		if (dataDef == null) {
			addErrorInfo("\u4E0D\u5B58\u5728\u7684\u5E10\u5957\u9879\u76EE\uFF01");
			return "input";
		}
		String title = dataDef.getEsddName();
		title = title != null ? title : "";
		title = (new StringBuilder()).append(title).append(
				"\u53D1\u653E\u5386\u53F2\u5206\u6790").toString();
		String tmpTitle = "";
		if (StringUtils.isNotEmpty(bean.getDepartmentId())) {
			Department dep = (Department) salaryReportBo.loadObject(
					Department.class, bean.getDepartmentId(), null);
			if (dep != null)
				tmpTitle = dep.getDepartmentName();
		}
		if (StringUtils.isNotEmpty(bean.getJobgradeId())) {
			Jobgrade grade = (Jobgrade) salaryReportBo.loadObject(
					Jobgrade.class, bean.getJobgradeId(), null);
			if (null != grade)
				tmpTitle = (new StringBuilder()).append(tmpTitle).append(
						grade.getJobGradeName()).toString();
		}
		title = (new StringBuilder()).append(tmpTitle).append(title).toString();
		java.util.List reportItems = salaryReportBo
				.getSalaryReportHistory(bean);
		Map params = new HashMap();
		params.put("reportObjects", reportItems);
		params.put("title", title);
		request.setAttribute("_params", params);
		request.setAttribute("_report",
				"/report/compensation/salary_analysis_history.rptdesign");
		return "success";
	}

	public SalaryReportParamBean getBean() {
		return bean;
	}

	public void setBean(SalaryReportParamBean bean) {
		this.bean = bean;
	}

	public String getReportFormat() {
		return reportFormat;
	}

	public void setReportFormat(String reportFormat) {
		this.reportFormat = reportFormat;
	}

	private String reportFormat;
	private SalaryReportParamBean bean;
	private static final String REPORT_FILE_LOCATION = "/report/compensation/salary_analysis_history.rptdesign";
}
